package cn.com.huzo.service.master.impl;

import cn.com.huzo.model.entities.master.CustomerGroupMemberCustomerEntity;
import huzofw.service.impl.GenericManagerImpl;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import cn.com.huzo.service.master.CustomerGroupMemberCustomerManager;
import cn.com.huzo.repository.master.CustomerGroupMemberCustomerRepository;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import huzofw.model.entities.paginator.Paginator;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.collections4.MapUtils;

/**
 * 客户组成员客户管理服务对象类实现。
 *
 * @author 吴旭
 */
@Service("CustomerGroupMemberCustomerManager")
public class CustomerGroupMemberCustomerManagerImpl extends GenericManagerImpl<CustomerGroupMemberCustomerEntity, HashMap>
        implements CustomerGroupMemberCustomerManager {

    @Resource(name = "CustomerGroupMemberCustomerRepository")
    private CustomerGroupMemberCustomerRepository _dao;

    /**
     * {@inheritDoc}
     *
     * @throws java.lang.Exception
     */
    @Transactional
    @Override
    public Map<String, Object> fetchForPage(Map<String, Object> criterion) throws Exception {
        Map<String, Object> _result = new HashMap();

        Long offset = (long) (MapUtils.getInteger(criterion, "Limit") * (MapUtils.getInteger(criterion, "PageIndex") - 1));
        long total = _dao.fetchForCount(criterion);

        if (total > 0) {
            if (offset >= total) {
                offset = (total - MapUtils.getInteger(criterion, "Limit")) < 0 ? 0 : (total - MapUtils.getInteger(criterion, "Limit"));
            }
            criterion.put("Offset", offset);
            List<CustomerGroupMemberCustomerEntity> itemList = _dao.fetchForPage(criterion);
            Paginator<CustomerGroupMemberCustomerEntity> paginator = new Paginator<>(itemList, offset, MapUtils.getInteger(criterion, "Limit"), total);

            _result.put("TotalRecordCount", paginator.getTotal());
            _result.put("TotalPages", paginator.getTotalPages());
            _result.put("PageIndex", paginator.getPageIndex());
            _result.put("DataList", paginator.getItemList());
            _result.put("Success", true);
        } else {
            _result.put("Success", false);
        }

        return _result;
    }

    /**
     * {@inheritDoc}
     *
     * @throws java.lang.Exception
     */
    @Transactional
    @Override
    public List<CustomerGroupMemberCustomerEntity> fetchByCustomerId(Long customerId) throws Exception {
        return _dao.fetchByCustomerId(customerId);
    }

    /**
     * {@inheritDoc}
     *
     * @throws java.lang.Exception
     */
    @Transactional
    @Override
    public Map<String, Object> insert(Map<String, Object> criterion) throws Exception {
        Map<String, Object> _result = new HashMap();

        ObjectMapper mapper = new ObjectMapper();
        List<CustomerGroupMemberCustomerEntity> itemList = mapper.convertValue(MapUtils.getObject(criterion, "ItemList"), new TypeReference<List<CustomerGroupMemberCustomerEntity>>() {
        });

        if (!itemList.isEmpty()) {
            // 插入
            Integer rowsAffected = _dao.batchInsert(itemList);
            if (rowsAffected > 0) {
                if (MapUtils.getBoolean(criterion, "IsPageList") == true) {
                    Map<String, Object> params = new HashMap();
                    params.put("GroupId", MapUtils.getLong(criterion, "GroupId"));
                    params.put("Limit", MapUtils.getInteger(criterion, "Limit02"));
                    params.put("PageIndex", MapUtils.getInteger(criterion, "PageIndex02"));

                    _result.put("LinesList", fetchForPage(params));
                } else {
                    _result.put("LinesList", fetchByCustomerId(itemList.get(0).getCustomerId()));
                }
                _result.put("Success", true);
            } else {
                _result.put("Success", false);
            }
        } else {
            _result.put("Success", false);
        }

        return _result;
    }

    /**
     * {@inheritDoc}
     *
     * @throws java.lang.Exception
     */
    @Transactional
    @Override
    public Map<String, Object> delete(Map<String, Object> criterion) throws Exception {
        Map<String, Object> _result = new HashMap();

        // 第一步删除
        Map<String, Object> params = new HashMap();
        params.put("CustomerId", MapUtils.getLong(criterion, "CustomerId"));
        params.put("GroupId", MapUtils.getLong(criterion, "GroupId"));
        Integer rowsAffected = _dao.delete((HashMap) params);
        // 第二步，删除后返回值
        if (rowsAffected > 0) {
            if (MapUtils.getBoolean(criterion, "IsPageList") == true) {
                params.clear();
                params.put("GroupId", MapUtils.getLong(criterion, "GroupId"));
                params.put("Limit", MapUtils.getInteger(criterion, "Limit02"));
                params.put("PageIndex", MapUtils.getInteger(criterion, "PageIndex02"));

                _result.put("LinesList", fetchForPage(params));
            } else {
                _result.put("LinesList", fetchByCustomerId(MapUtils.getLong(criterion, "CustomerId")));
            }
            _result.put("Success", true);
        } else {
            _result.put("Success", false);
        }

        return _result;
    }

    /**
     * {@inheritDoc}
     *
     * @throws java.lang.Exception
     */
    @Transactional
    @Override
    public Map<String, Object> batchDelete(Map<String, Object> criterion) throws Exception {
        Map<String, Object> _result = new HashMap();

        ObjectMapper mapper = new ObjectMapper();
        List<CustomerGroupMemberCustomerEntity> itemList = mapper.convertValue(MapUtils.getObject(criterion, "ItemList"), new TypeReference<List<CustomerGroupMemberCustomerEntity>>() {
        });

        if (!itemList.isEmpty()) {
            Integer rowsAffected = _dao.batchDelete(itemList);
            if (rowsAffected > 0) {
                Map<String, Object> params = new HashMap();
                params.put("GroupId", MapUtils.getLong(criterion, "GroupId"));
                params.put("Limit", MapUtils.getInteger(criterion, "Limit02"));
                params.put("PageIndex", MapUtils.getInteger(criterion, "PageIndex02"));

                _result.put("LinesList", fetchForPage(params));
                _result.put("Success", true);
            } else {
                _result.put("Success", false);
            }
        } else {
            _result.put("Success", false);
        }

        return _result;
    }
}
